package com.tongcao.cn.service.jenkins;

import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.model.ComputerSet;
import com.offbytwo.jenkins.model.Job;
import com.offbytwo.jenkins.model.View;
import org.dom4j.DocumentException;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

/**
 * @author:tongcao
 * @since:2019/8/5 14:15
 * Description:
 */
public interface IJenkinsService {


    /**
     * 取得全部job
     *
     * @return
     * @throws IOException
     */
    List<Job> getJob() throws IOException;


    /**
     * 取得job配置
     *
     * @param jobName job名
     * @return
     * @throws IOException
     */
    String getJobXml(String jobName) throws IOException;

    /**
     * 取得job
     *
     * @param jobName job名
     * @return
     * @throws IOException
     */
    Job getJob(String jobName) throws IOException;

    /**
     * 创建job
     *
     * @param jobName      job名
     * @param xmlConfigStr 配置
     * @return
     * @throws URISyntaxException
     * @throws DocumentException
     * @throws IOException
     */
    String createJob(String jobName, String xmlConfigStr) throws URISyntaxException, DocumentException, IOException;


    /**
     * 更新job配置
     *
     * @param jobName      job名
     * @param xmlConfigStr 配置
     * @return
     * @throws IOException
     */
    void updJobXml(String jobName, String xmlConfigStr) throws IOException;


    /**
     * 删除job
     *
     * @param jobName
     * @throws IOException
     */
    void deleteJob(String jobName) throws IOException;


    /**
     * 构建任务
     *
     * @param jobName job名字
     * @return
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    Integer buildByJobName(String jobName) throws IOException, InterruptedException, URISyntaxException;

    /**
     * 参数构建任务
     *
     * @param jobName job名字
     * @param params  构建参数
     * @return 构建编号
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    Integer buildByJobNameWithParams(String jobName, Map<String, String> params) throws IOException, InterruptedException, URISyntaxException;

    /**
     * 取消执行中任务
     *
     * @param jobName job名字
     * @param buildId 编译ID
     * @return
     */
    boolean cancelBuild(String jobName, Integer buildId) throws IOException;

    /**
     * 取得构建log -text类型
     *
     * @param jobName  任务名
     * @param buildId  编译ID
     * @param textSite 文字位置
     * @return
     * @throws URISyntaxException
     * @throws IOException
     */
    JenkinsLog getLogResumeWithText(String jobName, Integer buildId, Long textSite) throws URISyntaxException, IOException;


    /**
     * 取得view
     *
     * @param viewName view名
     * @return
     * @throws IOException
     */
    View getView(String viewName) throws IOException;


    /**
     * 创建View
     *
     * @param viewName 视图名称
     */
    void createView(String viewName);

    /**
     * 删除视图
     *
     * @param viewName 视图名称
     * @throws IOException
     */
    void deleteView(String viewName) throws IOException;


    /**
     * 添加job到视图
     *
     * @param jobName  job名称
     * @param viewName 视图名称
     * @throws IOException
     */
    void addJob2View(String jobName, String viewName) throws IOException;

    /**
     * 取得构建log -html类型
     *
     * @param jobName  任务名
     * @param buildId  编译ID
     * @param textSite 文字位置
     * @return
     * @throws IOException
     */
    JenkinsLog getLogResumeWithHtml(String jobName, Integer buildId, Long textSite) throws IOException;

    ComputerSet getComputerSet();

    JenkinsServer getJenkinsServer() throws URISyntaxException;

    String getViewXml(String viewName);


    /**
     * 更新view
     * @param viewName
     * @param newViewName
     * @throws IOException
     */
    void changeViewName(String viewName, String newViewName) throws IOException;
}
